package com.shop.mall.controller;

import com.shop.mall.common.Result;
import com.shop.mall.entity.Admin;
import com.shop.mall.entity.Admin;
import com.shop.mall.entity.Admin;
import com.shop.mall.entity.User;
import com.shop.mall.service.AdminService;
import com.shop.mall.utils.JwtUtil;
import com.shop.mall.utils.Md5Util;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 商家 前端控制器
 * </p>
 *
 */
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;
    /**
     * 添加admin
     */
    @PostMapping("/add")
    @Operation(summary = "注册商家")
    public Result<String> addAdmin(@RequestBody Admin admin) {
        String md5Pwd = Md5Util.getMD5String(admin.getPassword());
        admin.setPassword(md5Pwd);
        if (admin.getMoney() == null){
            admin.setMoney(BigDecimal.ZERO);
        }
        return adminService.add(admin);
    }


    @PostMapping("/login")
    @Operation(summary = "商家登录")
    @Parameters({
            @Parameter(name = "loginName", description = "登录名", required = true, schema = @Schema(type = "sting")),
            @Parameter(name = "password", description = "密码", required = true, schema = @Schema(type = "sting"))
    })
    public Result<String> login(@RequestBody Admin admin) {
        if (admin == null || "".equals(admin.getLoginName()) || admin.getLoginName() == null || "".equals(admin.getPassword()) || admin.getPassword() == null) {
            return Result.error("商家名密码不能为空");
        }
        // 检验商家名是否存在
        Admin eradmin = adminService.findByLoginName(admin.getLoginName());
        if (eradmin == null) {
            return Result.error("商家名不存在");
        }
        // 检验商家密码是否正确
        if (Md5Util.getMD5String(admin.getPassword()).equals(eradmin.getPassword())) {
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", eradmin.getId());
            claims.put("name", eradmin.getName());
            claims.put("loginName", eradmin.getLoginName());
            String token = JwtUtil.getToken(claims);
            return Result.success(token);
        }
        return Result.error("密码错误");
    }

    @GetMapping("/getById")
    @Operation(summary = "根据id查询商家信息")
    @Parameters({
            @Parameter(name = "Authorization", in = ParameterIn.HEADER, required = true, description = "token"),
            @Parameter(name = "id", required = true, description = "商家id")
    })
    public Result<Admin> getById(@RequestParam int id) {
        Admin user = adminService.getAdminById(id);
        if (user == null) {
            return Result.error("获取商家信息失败");
        }
        return Result.success(user);
    }

    /**
     * 修改商家信息
     * @param admin
     * @return Result<String>
     */
    @PutMapping("/update")
    @Operation(summary = "修改商家信息")
    @Parameter(name = "Authorization", in = ParameterIn.HEADER, required = true, description = "token")
    public Result<String> update(@RequestBody Admin admin) {
        boolean result = adminService.updateAdmin(admin);
        if (!result) {
            return Result.error("修改失败");
        }
        return Result.success("修改成功");
    }

    /**
     * 删除商家信息
     * @param id
     * @return Result<String>
     */
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除商家信息")
    @Parameters({
            @Parameter(name = "Authorization", in = ParameterIn.HEADER, required = true, description = "token"),
            @Parameter(name = "id", required = true, description = "商家id")
    })
    public Result<String> delete(@PathVariable int id) {
        boolean result = adminService.deleteById(id);
        if (!result) {
            return Result.error("删除失败");
        }
        return Result.success();
    }
}
